package cn.zealon.readingcloud.collection.service.impl;

import cn.zealon.readingcloud.collection.service.InputToMQSearvice;
import cn.zealon.readingcloud.common.pojo.book.Book;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class InputToMQSearviceImpl implements InputToMQSearvice {

    private static Logger log = LoggerFactory.getLogger(InputToMQSearviceImpl.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private final String queueName = "booksqueue";

    @Override
    public HashMap<String, Object> inputBookToMQ(String bookJson) {
        Book book = JSONObject.parseObject(bookJson,Book.class);
        log.debug(book.getBookName()+"被存入RabbitMQ");
        //消息存入队列
        rabbitTemplate.convertAndSend(queueName, book);

        HashMap<String, Object> map = new HashMap<>();
        map.put("success","true");
        map.put("message","book已经存入RabbitMQ");
        map.put("code","200");
        return map;
    }


}
